package dnl.confij;

import java.util.HashMap;
import java.util.Map;

import dnl.confij.c9s.BooleanConversion;
import dnl.confij.c9s.ColorConversion;
import dnl.confij.c9s.DoubleConversion;
import dnl.confij.c9s.IntegerConversion;
import dnl.confij.c9s.LongConversion;

/**
 * A registry for <code>ValueConverters</code>.
 * 
 * @author Daniel Or
 * 
 */
public class ConvertersRegistry {

	private Map<Class<?>, ValueConverter> converters = new HashMap<Class<?>, ValueConverter>();

	private static ConvertersRegistry instance;

	public static synchronized ConvertersRegistry getInstance() {
		if (instance == null) {
			instance = new ConvertersRegistry();
			registerDefaultConverters();
		}
		return instance;
	}

	private static void registerDefaultConverters() {

		ValueConverter[] defaultConverters = { new ColorConversion(), new IntegerConversion(),
				new BooleanConversion(), new DoubleConversion(), new LongConversion() };

		for (ValueConverter converter : defaultConverters) {
			for (Class<?> type : converter.getSupportedTypes()) {
				instance.register(type, converter);
			}
		}

	}

	/**
	 * 
	 * @param clazz
	 * @param trasformation
	 */
	public void register(Class<?> clazz, ValueConverter converter) {
		converters.put(clazz, converter);
	}

	/**
	 * 
	 * @param clazz
	 */
	public void unRegister(Class<?> clazz) {
		converters.remove(clazz);
	}

	/**
	 * 
	 * @param clazz
	 * @return
	 */
	public ValueConverter getConverter(Class<?> clazz) {
		return converters.get(clazz);
	}

}
